/**
 *    Copyright (C) 2007-2009 DIVIS GmbH (http://www.divis-hagen.de/)
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License as published by the Free Software Foundation; either
 *    version 3 of the License, or (at your option) any later version.
 *
 *    This library is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *    Lesser General Public License for more details.
 *
 *    You should have received a copy of the GNU Lesser General
 *    Public License along with this library; if not, write to the
 *    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 *    Boston, MA 02110-1301 USA
 */

package de.divis_hagen.json.rs.client;

import de.divis_hagen.json.annotation.EntityName;
import de.divis_hagen.json.annotation.EntityRequest;
import javax.ws.rs.Path;


/**
 *
 * @author Aike J Sommer
 */
public class EntityInfo {

    private final Class entity;
    private final Class handler;
    private final String entityTypeName;
    private final String handlerTypeName;
    private final String handlerInstanceName;
    private final String rootPath;
    private final RequestInfo loadRequest;

    public EntityInfo(Class handler, Class entity, String basePackage) {
        this.entity = entity;
        this.handler = handler;
        entityTypeName = basePackage + (entity.isAnnotationPresent(EntityName.class) ? ((EntityName) entity.getAnnotation(EntityName.class)).value()
                : entity.getSimpleName());
        handlerTypeName = entityTypeName + "Handler";

        if (handler.isAnnotationPresent(Path.class)) {
            String path = ((Path) handler.getAnnotation(Path.class)).value();
            if (path.endsWith("/")) {
                path = path.substring(0, path.length() - 1);
            }
            rootPath = path.startsWith("/") || path.isEmpty() ? path : ("/" + path);
            if (path.startsWith("/")) {
                path = path.substring(1);
            }
            handlerInstanceName = path.replace("/", "_");
        } else {
            throw new IllegalArgumentException(handler.getName());
        }

        loadRequest = RequestInfo.find(EntityRequest.Type.Load, this);
    }

    public Class getEntity() {
        return entity;
    }

    public String getEntityTypeName() {
        return entityTypeName;
    }

    public String getHandlerTypeName() {
        return handlerTypeName;
    }

    public String getHandlerInstanceName() {
        return handlerInstanceName;
    }

    public String getRootPath() {
        return rootPath;
    }

    protected Class getHandler() {
        return handler;
    }

    public RequestInfo getLoadRequest() {
        return loadRequest;
    }

}
